// vim: set ft=groovy ts=4 sw=4:
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// *
// *              C E D A R
// *          S O L U T I O N S       "Software done right."
// *           S O F T W A R E
// *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// *
// * Copyright (c) 2013-2014 Kenneth J. Pronovici.
// * All rights reserved.
// *
// * This program is free software; you can redistribute it and/or
// * modify it under the terms of the Apache License, Version 2.0.
// * See LICENSE for more information about the licensing terms.
// *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
// *
// * Author   : Kenneth J. Pronovici <pronovic@ieee.org>
// * Language : Gradle (>= 1.7)
// * Project  : Secret Santa Exchange
// *
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// See: http://stackoverflow.com/questions/1163173/why-use-gradle-instead-of-ant-or-maven

buildscript {
    repositories {
        mavenLocal()   // Local Maven repository
        mavenCentral() // Maven Central repository
    }

    dependencies {
        classpath "com.googlecode.cedar-common:cedar-build:0.8.14"
    }
}

apply plugin: "cedarGwtOnGae"
apply plugin: "cedarBuild"
apply plugin: "cedarCopyright"
apply plugin: "cedarJavadoc"
apply plugin: "cedarAnalysis"
apply plugin: "projectEclipse"

cedarProperties.loadStandardProperties()

def gwtConfig = file(config_mercurialLabelResources).canonicalPath
cedarProperties.loadGwtProperties(gwtConfig, [ "system_applicationName", "system_versionNumber", "system_releaseDate", ])

compileJava.options.fork(memoryMaximumSize: config_javaCompilerMemory)
sourceCompatibility = JavaVersion.VERSION_1_6  // because GWT 2.7.x does not support Java 7
targetCompatibility = JavaVersion.VERSION_1_7  // because AppEngine is now 1.7-only

cedarLabel {
    enabled = config_mercurialLabelEnabled
    projectName = system_applicationName
    projectVersion = "${system_versionNumber} (${system_releaseDate})"
    repositories = [ projectDir, ]
    mercurialPath = config_mercurialPath
}

cedarCopyright {
    licensePattern = '^.*LICENSE$'
    sourcePatterns = [ '^.*\\.java$', '^.*\\.rb$', '^.*\\.feature$', '^.*\\.gradle$', '^.*build.xml$', ]
    repositories = [ projectDir, ]
    mercurialPath = config_mercurialPath
}

cedarJavadoc {
    title = "${system_applicationName} ${system_versionNumber}"
    output = "${project.docsDir}/javadoc"
    classpath = { project.configurations.runtime }
    srcDirs = { sourceSets.main.java.srcDirs }
    subpackages = [ "com", ]
}

cedarAnalysis {
    sourceFolders = [ "src", ]
    testFolders = [ "test", ]
}

projectEclipse {
    sourceFolders = [ "resources", "src", "test", "suites", ]  // in desired order
    suitesSourceDir = "gradle/src/suites"
    settingsSourceDir = "gradle/eclipse/settings"
    externalToolBuildersSourceDir = "gradle/eclipse/externalToolBuilders"
}

cedarGwtOnGae {
    appModuleName = config_appModuleName
    appEntryPoint = config_appEntryPoint
    appStartupUrl = config_appStartupUrl

    gwtCompilerMemory = config_gwtCompilerMemory
    unittestMemory = config_unittestMemory
    unittestPermgen = config_unittestPermgen
    clienttestMemory = config_clienttestMemory
    clienttestPermgen = config_clienttestPermgen

    devmodeServerMemory = config_devmodeServerMemory
    devmodeServerPermgen = config_devmodeServerPermgen
    devmodeCodeserverPort = config_devmodeCodeserverPort
    devmodeServerPort = config_devmodeServerPort
    serverWait = config_serverWait
    stopWait = config_stopWait

    xvfbRunPath = config_xvfbRunPath

    postGwt27 = true
    appEngineVersion = config_appEngineVersion
}

cedarCucumber {
    rubyInstallDir = config_rubyInstallDir
    cucumberDir = "acceptance"
    featuresSubdir = "cucumber"
    rubySubdir = "ruby"
    jrubyCucumberMemory = config_jrubyCucumberMemory
    cucumberFormatter = "Formatter::Cedar"
}

repositories {
    mavenLocal()   // Local Maven repository
    mavenCentral() // Maven Central repository
    maven { url "http://repository.springsource.com/maven/bundles/external" } // Spring Source bundles
}

sourceSets {
    main {
        java { srcDirs = [ "src" ] }
        resources { srcDirs = [ "src", "resources", ] }
    }

    test {
        java { srcDirs = [ "test", "gradle/src/suites", ] }
        resources { srcDirs = [ "test", "gradle/src/suites", "gradle/resources", ] }
        runtimeClasspath += files(sourceSets.main.java.srcDirs)
        runtimeClasspath += files(sourceSets.main.resources.srcDirs)
        runtimeClasspath += files(sourceSets.test.java.srcDirs)
    }

    devmode {
    }
}

configurations {
    // Used to set up runtime classpath for cedarAnalysis plugin
    analysis

    // We get the AppEngine jars directly from the SDK zip, to ease our burden with Eclipse
    all*.exclude group: "com.google.appengine", module: "appengine-api-1.0-sdk"

    // These transitive dependencies are not required for the application to work properly
    all*.exclude group: "asm", module: "asm"
    all*.exclude group: "commons-chain", module: "commons-chain"
    all*.exclude group: "commons-codec", module: "commons-codec"
    all*.exclude group: "commons-codec", module: "commons-codec"
    all*.exclude group: "commons-digester", module: "commons-digester"
    all*.exclude group: "commons-validator", module: "commons-validator"
    all*.exclude group: "dom4j", module: "dom4j"
    all*.exclude group: "javax.activation", module: "activation"
    all*.exclude group: "javax.servlet", module: "servlet-api"
    all*.exclude group: "javax.transaction", module: "transaction-api"
    all*.exclude group: "org.apache.struts"
    all*.exclude group: "org.json", module: "json"
    all*.exclude group: "org.sonatype.sisu.inject", module: "cglib"
    all*.exclude group: "org.springframework", module: "spring-support"
    all*.exclude group: "sslext", module: "sslext"
}

dependencies {
    gaeSdk "com.google.appengine:appengine-java-sdk:${config_appEngineVersion}"

    compile "com.google.gwt:gwt-servlet:${config_gwtVersion}"
    compile fileTree(dir: project.convention.plugins.cedarGwtOnGae.getAppEngineSdkDir(), includes: [ "lib/user/**/*.jar", ])

    compile "com.googlecode.cedar-common:cedar-common-util:${config_cedarCommonVersion}"
    compile "com.googlecode.cedar-common:cedar-common-gae:${config_cedarCommonVersion}"
    compile("com.googlecode.cedar-common:cedar-common-gwt:${config_cedarCommonVersion}") {
        exclude group: "com.google.gwt"       // proper version is providedRuntime, below
    }

    testCompile "com.googlecode.cedar-common:cpsuite:1.2.6"
    testCompile "com.googlecode.cedar-common:cedar-common-testutil:${config_cedarCommonVersion}"
    testCompile "com.googlecode.cedar-common:cedar-common-gwttestutil:${config_cedarCommonVersion}"
    testCompile("com.googlecode.cedar-common:cedar-common-gaetestutil:${config_cedarCommonVersion}") {
        exclude group: "com.google.appengine" // proper version is testCompile, below
    }

    testCompile "com.google.appengine:appengine-api-stubs:${config_appEngineVersion}"
    testCompile "com.google.appengine:appengine-testing:${config_appEngineVersion}"

    runtime("org.apache.xalan:com.springsource.org.apache.xalan:2.7.1") { transitive = false }
    runtime("org.apache.xerces:com.springsource.org.apache.xerces:2.9.1") { transitive = false }
    runtime "org.aspectj:aspectjrt:1.8.4"

    providedRuntime "com.google.gwt:gwt-dev:${config_gwtVersion}"
    providedRuntime "com.google.gwt:gwt-user:${config_gwtVersion}"

    devmodeRuntime fileTree(dir: project.convention.plugins.cedarGwtOnGae.getAppEngineSdkDir(), includes: [ "lib/appengine-tools-api.jar", ])

    analysis "org.codehaus.javancss:javancss:33.54"
}

project.tasks.javadoc.doFirst {
    throw new Exception("Do not use javadoc task, use generateJavadoc instead.")
}

task generateJavadoc(dependsOn: jdoc) << {
    copyJavadocToMercurial(config_mercurialJavadocProject, project, { name -> name })
}

